package com.lm.easyexcel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.lm.easyexcel.entry.UserData;

import java.util.ArrayList;
import java.util.List;

public class UserDataListener extends AnalysisEventListener<UserData> {

    private static final int BATCH_COUNT = 1000; // 每批处理1000条
    private final List<UserData> cache = new ArrayList<>();

    @Override
    public void invoke(UserData data, AnalysisContext context) {
        cache.add(data);
        if (cache.size() >= BATCH_COUNT) {
            saveData();
            cache.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData(); // 保存最后一批
    }

    private void saveData() {
        System.out.println("批量保存数据：" + cache.size() + " 条");
        // TODO: 调用 service 批量入库，推荐使用 MyBatis 批量 insert
    }
}

